<?php


namespace app\util;


class Camelize
{

    // 驼峰
    public static function camelize($uncamelized_words,$separator='_') {
        $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
        return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
    }

    // 驼峰
    public static function camelizeArray ($array) {
        $data = [];
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $data[self::camelize($key)] = self::camelizeArray($value);
            } else {
                $data[self::camelize($key)] = $value;
            }
        }
        return $data;
    }

    // 下划线
    public static function uncamelize($camelCaps,$separator='_') {
        return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
    }

    // 下划线
    public static function uncamelizeArray($array) {
        $data = [];
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $data[self::uncamelize($key)] = self::uncamelizeArray($value);
            } else {
                $data[self::uncamelize($key)] = $value;
            }
        }
        return $data;
    }
}